home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / osi / isode / dosisode / DOSISODE80.ZIP / ISODE8.WRK / COMPAT / LOGGER.C_N < prev    next >
Encoding:
Text File  |  1992-06-28  |  10.9 KB  |  617 lines

  1. /* logger.c - system logging routines */
  2.  
  3. #ifndef    lint
  4. static char *rcsid = "$Header: /xtel/isode/isode/compat/RCS/logger.c,v 9.0 1992/06/16 12:07:00 isode Rel $";
  5. #endif
  6.  
  7. /* 
  8.  * $Header: /xtel/isode/isode/compat/RCS/logger.c,v 9.0 1992/06/16 12:07:00 isode Rel $
  9.  *
  10.  *
  11.  * $Log: logger.c,v $
  12.  * Revision 9.0  1992/06/16  12:07:00  isode
  13.  * Release 8.0
  14.  *
  15.  */
  16.  
  17. /*
  18.  *                  NOTICE
  19.  *
  20.  *    Acquisition, use, and distribution of this module and related
  21.  *    materials are subject to the restrictions of a license agreement.
  22.  *    Consult the Preface in the User's Manual for the full terms of
  23.  *    this agreement.
  24.  *
  25.  */
  26.  
  27.  
  28. /* LINTLIBRARY */
  29.  
  30. #include <stdio.h>
  31. #include <varargs.h>
  32. #include "general.h"
  33. #include "manifest.h"
  34. #include "logger.h"
  35. #include "tailor.h"
  36.  
  37. #ifdef    NULL
  38. #undef    NULL
  39. #endif
  40. #include <sys/param.h>
  41. #ifndef    NULL
  42. #define    NULL    0
  43. #endif
  44. #include "sys.file.h"
  45. #include <sys/stat.h>
  46.  
  47. #ifndef    SYS5
  48. #include <syslog.h>
  49.  
  50. extern void closelog();
  51. #endif
  52.  
  53. /*   */
  54.  
  55. #define PKBUFSIZ 4096
  56. #ifndef    lint
  57. static
  58. #endif
  59. int  _ll_printf ();
  60.  
  61. struct ll_private {
  62.     int        ll_checks;
  63. #define    CHKINT    15        /* call ll_check 1 in every 15 uses... */
  64. };
  65.  
  66. static struct ll_private *llp = NULL;
  67. static IFP _ll_header_routine = ll_defmhdr;
  68.  
  69. long    lseek ();
  70. time_t  time();
  71.  
  72. /*   */
  73.  
  74. int    ll_open (lp)
  75. register LLog *lp;
  76. {
  77.     int        mask,
  78.         mode;
  79.     char   *bp,
  80.         buffer[PKBUFSIZ];
  81.  
  82.     if (llp == NULL
  83.         && (llp = (struct ll_private *)
  84.             calloc ((unsigned int) getdtablesize (),
  85.                 sizeof *llp)) == NULL)
  86.     goto you_lose;
  87.  
  88.     if (lp -> ll_file == NULLCP
  89.         || *lp -> ll_file == NULL) {
  90. you_lose: ;
  91.     (void) ll_close (lp);
  92.     lp -> ll_stat |= LLOGERR;
  93.     return NOTOK;
  94.     }
  95.  
  96.     lp -> ll_stat &= ~LLOGERR;
  97.  
  98.     if (lp -> ll_fd != NOTOK)
  99.     return OK;
  100.  
  101.     if (strcmp (lp -> ll_file, "-") == 0) {
  102.     lp -> ll_stat |= LLOGTTY;
  103.     return OK;
  104.     }
  105.  
  106.     (void) sprintf (bp = buffer, _isodefile (isodelogpath, lp -> ll_file),
  107.             getpid ());
  108.  
  109.     mode = O_WRONLY | O_APPEND;
  110.     if (lp -> ll_stat & LLOGCRT)
  111.     mode |= O_CREAT;
  112.  
  113.     mask = umask (~0666);
  114.     lp -> ll_fd = open (bp, mode, 0666);
  115.     (void) umask (mask);
  116.  
  117.     if (ll_check (lp) == NOTOK)
  118.     return (NOTOK);
  119.     if (lp -> ll_fd != NOTOK)
  120.     llp[lp -> ll_fd].ll_checks = CHKINT;
  121.  
  122.     return (lp -> ll_fd != NOTOK ? OK : NOTOK);
  123. }
  124.  
  125. /*   */
  126.  
  127. int    ll_close (lp)
  128. register LLog *lp;
  129. {
  130.     int        status;
  131.     
  132.     if (lp -> ll_fd == NOTOK)
  133.     return OK;
  134.  
  135.     status = close (lp -> ll_fd);
  136.     lp -> ll_fd = NOTOK;
  137.  
  138.     return status;
  139. }
  140.  
  141. /*   */
  142.  
  143. #ifndef    lint
  144. int    ll_log (va_alist)
  145. va_dcl
  146. {
  147.     int        event,
  148.         result;
  149.     LLog   *lp;
  150.     va_list ap;
  151.  
  152.     va_start (ap);
  153.  
  154.     lp = va_arg (ap, LLog *);
  155.     event = va_arg (ap, int);
  156.  
  157.     result = _ll_log (lp, event, ap);
  158.  
  159.     va_end (ap);
  160.  
  161.     return result;
  162. }
  163. #else
  164. /* VARARGS4 */
  165.  
  166. int    ll_log (lp, event, what, fmt)
  167. LLog   *lp;
  168. int    event;
  169. char   *what,
  170.        *fmt;
  171. {
  172.     return ll_log (lp, event, what, fmt);
  173. }
  174. #endif
  175.  
  176. /*   */
  177.  
  178. int    _ll_log (lp, event, ap)    /* what, fmt, args ... */
  179. register LLog *lp;
  180. int    event;
  181. va_list    ap;
  182. {
  183.     int        cc,
  184.         status;
  185.     register char *bp;
  186.     char   *what,
  187.         buffer[PKBUFSIZ];
  188.  
  189.     if (!(lp -> ll_events & event))
  190.     return OK;
  191.  
  192.     bp = buffer;
  193.  
  194.     /* Create header */
  195.     (*_ll_header_routine)(bp, lp -> ll_hdr, lp -> ll_dhdr);
  196.  
  197.     bp += strlen (bp);
  198.  
  199.     what = va_arg (ap, char *);
  200.  
  201.     _asprintf (bp, what, ap);
  202.  
  203. #ifndef    SYS5
  204.     if (lp -> ll_syslog & event) {
  205.     int    priority;
  206.  
  207.     switch (event) {
  208.         case LLOG_FATAL:
  209.             priority = LOG_ERR;
  210.         break;
  211.  
  212.         case LLOG_EXCEPTIONS:
  213.         priority = LOG_WARNING;
  214.         break;
  215.  
  216.         case LLOG_NOTICE:
  217.             priority = LOG_INFO;
  218.         break;
  219.  
  220.         case LLOG_PDUS:
  221.         case LLOG_TRACE:
  222.         case LLOG_DEBUG:
  223.             priority = LOG_DEBUG;
  224.         break;
  225.  
  226.         default:
  227.         priority = LOG_NOTICE;
  228.         break;
  229.     }
  230.  
  231.     (void) syslog (priority, "%s", buffer + 13);
  232.  
  233.     if (lp -> ll_stat & LLOGCLS)
  234.         (void) closelog ();
  235.     }
  236. #endif
  237.  
  238.     if (!(lp -> ll_stat & LLOGTTY)
  239.         && lp -> ll_fd == NOTOK
  240.         && strcmp (lp -> ll_file, "-") == 0)
  241.     lp -> ll_stat |= LLOGTTY;
  242.  
  243.     if (lp -> ll_stat & LLOGTTY) {
  244.     (void) fflush (stdout);
  245.  
  246.     if (lp -> ll_fd != NOTOK)
  247.         (void) fprintf (stderr, "LOGGING: ");
  248.     (void) fputs (bp, stderr);
  249.     (void) fputc ('\n', stderr);
  250.     (void) fflush (stderr);
  251.     }
  252.     bp += strlen (bp);
  253.  
  254.     if (lp -> ll_fd == NOTOK) {
  255.     if ((lp -> ll_stat & (LLOGERR | LLOGTTY)) == (LLOGERR | LLOGTTY))
  256.         return OK;
  257.     if (ll_open (lp) == NOTOK)
  258.         return NOTOK;
  259.     }
  260.     else
  261.     if ((!llp || llp[lp -> ll_fd].ll_checks-- < 0)
  262.         && ll_check (lp) == NOTOK)
  263.         return NOTOK;
  264.  
  265.     *bp++ = '\n', *bp = NULL;
  266.     cc = bp - buffer;
  267.  
  268.     if ((status = write (lp -> ll_fd, buffer, cc)) != cc) {
  269.     if (status == NOTOK) {
  270.         (void) ll_close (lp);
  271. error: ;
  272.         lp -> ll_stat |= LLOGERR;
  273.         return NOTOK;
  274.     }
  275.  
  276.     status = NOTOK;
  277.     }
  278.     else
  279.     status = OK;
  280.  
  281.     if ((lp -> ll_stat & LLOGCLS) && ll_close (lp) == NOTOK)
  282.     goto error;
  283.  
  284.     return status;
  285. }
  286.  
  287. /*   */
  288.  
  289. void    ll_hdinit (lp, prefix)
  290. register LLog *lp;
  291. char   *prefix;
  292. {
  293.     register char  *cp,
  294.            *up;
  295.     char    buffer[PKBUFSIZ],
  296.         user[10];
  297.  
  298.     if (prefix == NULLCP) {
  299.     if ((lp -> ll_stat & LLOGHDR) && strlen (lp -> ll_hdr) == 25)
  300.         (cp = lp -> ll_hdr)[8] = NULL;
  301.     else
  302.         cp = "unknown";
  303.     }
  304.     else {
  305.     if ((cp = rindex (prefix, '/')))
  306.         cp++;
  307.     if (cp == NULL || *cp == NULL)
  308.         cp = prefix;
  309.     }
  310.  
  311.     if ((up = getenv ("USER")) == NULLCP
  312.         && (up = getenv ("LOGNAME")) == NULLCP) {
  313.     (void) sprintf (user, "#%d", getuid ());
  314.     up = user;
  315.     }
  316.     (void) sprintf (buffer, "%-8.8s %05d (%-8.8s)",
  317.             cp, getpid () % 100000, up);
  318.  
  319.     if (lp -> ll_stat & LLOGHDR)
  320.     free (lp -> ll_hdr);
  321.     lp -> ll_stat &= ~LLOGHDR;
  322.  
  323.     if ((lp -> ll_hdr = malloc ((unsigned) (strlen (buffer) + 1))) == NULLCP)
  324.     return;
  325.  
  326.     (void) strcpy (lp -> ll_hdr, buffer);
  327.     lp -> ll_stat |= LLOGHDR;
  328. }
  329.  
  330. /*   */
  331.  
  332. void    ll_dbinit (lp, prefix)
  333. register LLog *lp;
  334. char   *prefix;
  335. {
  336.     register char  *cp;
  337.     char    buffer[PKBUFSIZ];
  338.  
  339.     ll_hdinit (lp, prefix);
  340.  
  341.     if (prefix) {
  342.     if ((cp = rindex (prefix, '/')))
  343.         cp++;
  344.     if (cp == NULL || *cp == NULL)
  345.         cp = prefix;
  346.  
  347.     (void) sprintf (buffer, "./%s.log", cp);
  348.  
  349.     if ((lp -> ll_file = malloc ((unsigned) (strlen (buffer) + 1)))
  350.             == NULLCP)
  351.         return;
  352.  
  353.     (void) strcpy (lp -> ll_file, buffer);
  354.     }
  355.  
  356.     lp -> ll_events |= LLOG_ALL;
  357.     lp -> ll_stat |= LLOGTTY;
  358. }
  359.  
  360. /*   */
  361.  
  362. #ifndef    lint
  363. int    ll_printf (va_alist)
  364. va_dcl
  365. {
  366.     int        result;
  367.     LLog    *lp;
  368.     va_list ap;
  369.  
  370.     va_start (ap);
  371.  
  372.     lp = va_arg (ap, LLog *);
  373.  
  374.     result = _ll_printf (lp, ap);
  375.  
  376.     va_end (ap);
  377.  
  378.     return result;
  379. }
  380. #else
  381. /* VARARGS2 */
  382.  
  383. int    ll_printf (lp, fmt)
  384. LLog   *lp;
  385. char   *fmt;
  386. {
  387.     return ll_printf (lp, fmt);
  388. }
  389. #endif
  390.  
  391. /*   */
  392.  
  393. #ifndef    lint
  394. static
  395. #endif
  396. int  _ll_printf (lp, ap)        /* fmt, args ... */
  397. register LLog *lp;
  398. va_list    ap;
  399. {
  400.     int        cc,
  401.         status;
  402.     register char   *bp;
  403.     char     buffer[PKBUFSIZ];
  404.     char    *fmt;
  405.     va_list fp;
  406.  
  407.     fp = ap;
  408.  
  409.     fmt = va_arg (fp, char *);
  410.     if (strcmp (fmt, "%s") != 0) {
  411.     bp = buffer;
  412.     _asprintf (bp, NULLCP, ap);
  413.     }
  414.     else {
  415.     bp = NULL;
  416.     fmt = va_arg (fp, char *);
  417.     }
  418.  
  419.     if (!(lp -> ll_stat & LLOGTTY)
  420.         && lp -> ll_fd == NOTOK
  421.         && strcmp (lp -> ll_file, "-") == 0)
  422.     lp -> ll_stat |= LLOGTTY;
  423.  
  424.     if (lp -> ll_stat & LLOGTTY) {
  425.     (void) fflush (stdout);
  426.  
  427.     if (bp)
  428.         (void) fputs (bp, stderr);
  429.     else
  430.         (void) fputs (fmt, stderr);
  431.     (void) fflush (stderr);
  432.     }
  433.     if (bp)
  434.     bp += strlen (bp);
  435.  
  436.     if (lp -> ll_fd == NOTOK) {
  437.     if ((lp -> ll_stat & (LLOGERR | LLOGTTY)) == (LLOGERR | LLOGTTY))
  438.         return OK;
  439.     if (ll_open (lp) == NOTOK)
  440.         return NOTOK;
  441.     }
  442.     else
  443.     if ((!llp || llp[lp -> ll_fd].ll_checks-- < 0)
  444.         && ll_check (lp) == NOTOK)
  445.         return NOTOK;
  446.  
  447.     if (bp)
  448.     cc = bp - buffer;
  449.     else
  450.     cc = strlen (fmt);
  451.  
  452.     if ((status = write (lp -> ll_fd, bp ? buffer : fmt, cc)) != cc) {
  453.     if (status == NOTOK) {
  454.         (void) ll_close (lp);
  455.         lp -> ll_stat |= LLOGERR;
  456.         return NOTOK;
  457.     }
  458.  
  459.     status = NOTOK;
  460.     }
  461.     else
  462.     status = OK;
  463.  
  464.     return status;
  465. }
  466.  
  467. /*   */
  468.  
  469. int    ll_sync (lp)
  470. register LLog *lp;
  471. {
  472.     if (lp -> ll_stat & LLOGCLS)
  473.     return ll_close (lp);
  474.  
  475.     return OK;
  476. }
  477.  
  478. /*   */
  479.  
  480. #ifndef    lint
  481. char   *ll_preset (va_alist)
  482. va_dcl
  483. {
  484.     va_list ap;
  485.     static char buffer[PKBUFSIZ];
  486.  
  487.     va_start (ap);
  488.  
  489.     _asprintf (buffer, NULLCP, ap);
  490.  
  491.     va_end (ap);
  492.  
  493.     return buffer;
  494. }
  495. #else
  496. /* VARARGS1 */
  497.  
  498. char   *ll_preset (fmt)
  499. char   *fmt;
  500. {
  501.     return ll_preset (fmt);
  502. }
  503. #endif
  504.  
  505. /*   */
  506.  
  507. int    ll_check (lp)
  508. register LLog *lp;
  509. {
  510. #ifndef    BSD42
  511.     int        fd;
  512.     char    buffer[PKBUFSIZ];
  513. #endif
  514.     long    size;
  515.     struct stat st;
  516.  
  517.     if ((size = lp -> ll_msize) <= 0)
  518.     return OK;
  519.  
  520.     if (llp && lp -> ll_fd != NOTOK)
  521.     llp[lp -> ll_fd].ll_checks = CHKINT;
  522.     if (lp -> ll_fd == NOTOK
  523.         || (fstat (lp -> ll_fd, &st) != NOTOK
  524.             && st.st_size < (size <<= 10)))
  525.     return OK;
  526.  
  527.     if (!(lp -> ll_stat & LLOGZER)) {
  528.     (void) ll_close (lp);
  529.  
  530. #ifndef    BSD42
  531. error: ;
  532. #endif
  533.     lp -> ll_stat |= LLOGERR;
  534.     return NOTOK;
  535.     }
  536.  
  537. #ifdef    BSD42
  538. #ifdef    SUNOS4
  539.     (void) ftruncate (lp -> ll_fd, (off_t) 0);
  540. #else
  541.     (void) ftruncate (lp -> ll_fd, 0);
  542. #endif
  543.     (void) lseek (lp -> ll_fd, 0L, 0);
  544.     return OK;
  545. #else
  546.     (void) sprintf (buffer, _isodefile (isodelogpath, lp -> ll_file),
  547.             getpid ());
  548.     if ((fd = open (buffer, O_WRONLY | O_APPEND | O_TRUNC)) == NOTOK)
  549.     goto error;
  550.     (void) close (fd);
  551.     return OK;
  552. #endif
  553. }
  554.  
  555. /*   */
  556.  
  557. /*
  558.  * ll_defmhdr - Default "make header" routine.
  559.  */
  560. int    ll_defmhdr(bufferp, headerp, dheaderp)
  561. char    *bufferp;        /* Buffer pointer */
  562. char    *headerp;        /* Static header string */
  563. char    *dheaderp;        /* Dynamic header string */
  564. {
  565.     time_t    clock;
  566.     register struct tm *tm;
  567.  
  568.     (void) time (&clock);
  569.     tm = localtime (&clock);
  570.  
  571.     (void) sprintf (bufferp, "%2d/%2d %2d:%02d:%02d %s %s ",
  572.             tm -> tm_mon + 1, tm -> tm_mday,
  573.             tm -> tm_hour, tm -> tm_min, tm -> tm_sec,
  574.             headerp ? headerp : "",
  575.             dheaderp ? dheaderp : "");
  576.     return OK;
  577. }
  578.  
  579. /*   */
  580.  
  581. /*
  582.  * ll_setmhdr - Set "make header" routine, overriding default.
  583.  */
  584. IFP    ll_setmhdr (make_header_routine) 
  585. IFP    make_header_routine;
  586. {
  587.     IFP result = _ll_header_routine;
  588.  
  589.     _ll_header_routine = make_header_routine;
  590.  
  591.     return result;
  592.  
  593. }
  594.  
  595.  
  596. #ifdef ULTRIX_X25
  597. #ifdef ULTRIX_X25_DEMSA
  598.  
  599. char * CAT(x,y)
  600. char * x;
  601. char * y;
  602. {
  603.   if ( strlen(x)+strlen(y)-2 > BUFSIZ-1 )
  604.        return (char *) y;
  605.   else
  606.   {
  607.  
  608.     strcpy(our_global_buffer,x);
  609.     strcat(our_global_buffer,y);
  610.  
  611.     return (char *) our_global_buffer;
  612.   }
  613. }
  614.  
  615. #endif
  616. #endif
  617.